package com.appkubes.fintech.restful.application.afu.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.appkubes.fintech.restful.application.afu.entity.AFuFraudDto;
import com.appkubes.fintech.restful.application.afu.entity.AFuRequestParam;
import com.appkubes.fintech.restful.application.afu.entity.BaseAfuDto;
import com.google.gson.Gson;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;


public class AFuUtils {
    //请求致诚阿福群星接口地址
    private static String api_name = "decision.report.pro.mbt.api";
    private static String api_name_afrud = "fraud.screening.advance.api";
    private static String url = "https://starapi.afufintech.com/submit";
    private static String user_name = "zhongzheng";//用户名
    private static String sign = "c5e3e337086e86df";//rc4秘钥


    public static BaseAfuDto getAfuScore(AFuRequestParam param) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //用户名
        nameValuePairs.add(new BasicNameValuePair("user_name", user_name));
        //rc4秘钥
        nameValuePairs.add(new BasicNameValuePair("sign", sign));
        //接口业务码 参考接口文档
        nameValuePairs.add(new BasicNameValuePair("api_name", api_name));
        // 查询原因
        // LOAN_AUDIT：贷款审批 LOAN_MANAGE：贷后管理 CREDIT_CARD_AUDIT：信用卡审批
        // GUARANTEE_AUDIT:担保资格审查 PRE_GUARANTEE_AUDIT:保前审查
        nameValuePairs.add(new BasicNameValuePair("query_reason", "LOAN_AUDIT"));

        nameValuePairs.add(new BasicNameValuePair("params", new Gson().toJson(param)));
        String result = HttpClientUtil.doPost(url, nameValuePairs, "utf-8");
        return new Gson().fromJson(result, BaseAfuDto.class);
    }

    public static AFuFraudDto getAfuFurad(AFuRequestParam param) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //用户名
        nameValuePairs.add(new BasicNameValuePair("user_name", user_name));
        //rc4秘钥
        nameValuePairs.add(new BasicNameValuePair("sign", sign));
        //接口业务码 参考接口文档
        nameValuePairs.add(new BasicNameValuePair("api_name", api_name_afrud));
        // 查询原因
        // LOAN_AUDIT：贷款审批 LOAN_MANAGE：贷后管理 CREDIT_CARD_AUDIT：信用卡审批
        // GUARANTEE_AUDIT:担保资格审查 PRE_GUARANTEE_AUDIT:保前审查
        nameValuePairs.add(new BasicNameValuePair("query_reason", "LOAN_AUDIT"));

        nameValuePairs.add(new BasicNameValuePair("params", new Gson().toJson(param)));
        String result = HttpClientUtil.doPost(url, nameValuePairs, "utf-8");
        return new Gson().fromJson(result, AFuFraudDto.class);
    }


    //请求平台
    public static void main(String[] args) {

        String re = "{\"fraudScore\":\"76\",\"fraudLevel\":\"4\",\"riskResult\":[{\"dataType\":\"OTHER\",\"riskDetail\":\"ARREARS\",\"riskItemType\":\"ID_NO\",\"riskTime\":\"2019\",\"riskItemValue\":\"3101**********0428\"}]}";

        AFuFraudDto.DataBean s = new Gson().fromJson(re, AFuFraudDto.DataBean.class);
        System.out.println(s.getFraudLevel());
        System.out.println(s.getFraudScore());
        System.out.println(s.getRiskResult());
/*        try {
            //开始查询
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            //用户名
            nameValuePairs.add(new BasicNameValuePair("user_name", user_name));
            //rc4秘钥
            nameValuePairs.add(new BasicNameValuePair("sign", sign));
            //接口业务码 参考接口文档
            nameValuePairs.add(new BasicNameValuePair("api_name", api_name_afrud));
            // 查询原因
            // LOAN_AUDIT：贷款审批 LOAN_MANAGE：贷后管理 CREDIT_CARD_AUDIT：信用卡审批
            // GUARANTEE_AUDIT:担保资格审查 PRE_GUARANTEE_AUDIT:保前审查
            nameValuePairs.add(new BasicNameValuePair("query_reason", "LOAN_AUDIT"));
            //查询条件,格式为 json 格式
            System.out.println("request params value is :"+gennerateRequetParam().toJSONString());
            nameValuePairs.add(new BasicNameValuePair("params", gennerateRequetParam().toJSONString()));
            String result= HttpClientUtil.doPost(url, nameValuePairs, "utf-8");
            //查询完成
            System.out.println("返回结果为:"+result);
        }catch (Exception e){
            e.printStackTrace();
        }*/
    }

/*    //请求平台
    public static void main(String[] args) {
        try {
            //开始查询
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            //用户名
            nameValuePairs.add(new BasicNameValuePair("user_name", user_name));
            //rc4秘钥
            nameValuePairs.add(new BasicNameValuePair("sign", sign));
            //接口业务码 参考接口文档
            nameValuePairs.add(new BasicNameValuePair("api_name", api_name));
            // 查询原因
            // LOAN_AUDIT：贷款审批 LOAN_MANAGE：贷后管理 CREDIT_CARD_AUDIT：信用卡审批
            // GUARANTEE_AUDIT:担保资格审查 PRE_GUARANTEE_AUDIT:保前审查
            nameValuePairs.add(new BasicNameValuePair("query_reason", "LOAN_AUDIT"));
            //查询条件,格式为 json 格式
            System.out.println("request params value is :" + gennerateRequetParam().toJSONString());
            nameValuePairs.add(new BasicNameValuePair("params", gennerateRequetParam().toJSONString()));
            String result = HttpClientUtil.doPost(url, nameValuePairs, "utf-8");
            //查询完成
            System.out.println("返回结果为:" + result);
            System.out.println("返回结果为:" + new Gson().fromJson(result, BaseAfuDto.class).getCode());
            System.out.println("返回结果为:" + new Gson().fromJson(result, BaseAfuDto.class).getMsg());
            System.out.println("返回结果为:" + new Gson().fromJson(result, BaseAfuDto.class).getData().getDecisionSuggest());
            //   System.out.println("返回结果为:" +result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/


    public static JSONObject gennerateRequetParam() {
        JSONObject dataJson = new JSONObject();
        dataJson.put("id_no", "321281198912287974");//被查询人身份证号 18 位以内
        dataJson.put("name", "华明泉");//被查询人姓名 2-30 位
        dataJson.put("amount_business", "2");//业务类型默认填写0
        dataJson.put("mobile", "15802140163,18795868113");//被查询人姓名 2-30 位
        dataJson.put("bank_no", "6222600640011810331");
        JSONObject corp_addr_json = new JSONObject();
        corp_addr_json.put("address", "温特莱B座1910");
        corp_addr_json.put("city", "北京市");
        corp_addr_json.put("county", "朝阳区");
        corp_addr_json.put("province", "北京市");
        dataJson.put("corp_addr", corp_addr_json);
        dataJson.put("corp_name", "宜信");
        dataJson.put("corp_tel", "010-61300110");
        JSONArray contacts = new JSONArray();
        JSONObject cpntactJson = new JSONObject();
        cpntactJson.put("contact_id_no", "370633197005042513");
        cpntactJson.put("contact_mobile", "15002035914");
        cpntactJson.put("contact_name", "高求");
        cpntactJson.put("contact_type", "SPOUSE");
        contacts.add(cpntactJson);
        dataJson.put("contacts", contacts);
        dataJson.put("email", "123qq@qq.com");
        JSONObject family_addrjson = new JSONObject();
        family_addrjson.put("address", "温特莱B座1910");
        family_addrjson.put("city", "北京市");
        family_addrjson.put("county", "朝阳区");
        family_addrjson.put("province", "北京市");
        dataJson.put("family_addr", family_addrjson);
        dataJson.put("family_tel", "010-61300110");
        dataJson.put("qq", "532537296");
        return dataJson;
    }
}
